//
//  UserListViewController.m
//  STQuickKitDemo
//
//  Created by yls on 13-11-27.
//  Copyright (c) 2013年 yls. All rights reserved.
//

#import "UserListViewController.h"
#import "AddUserViewController.h"
#import "UserDetailViewController.h"
#import "STUser.h"

@interface UserListViewController ()

@property (assign, nonatomic) NSInteger selectedRow;
@property (strong, nonatomic) NSMutableArray *users;

@end

@implementation UserListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser:)];
    [self.navigationItem setRightBarButtonItems:@[rightBtnItem]];
    
    _tableView.editing = YES;

    // 添加默认用户
    if (![STUser existDbObjectsWhere:@"_id=0"]) {
        STUser *defaultUser = [[STUser alloc] init];
        defaultUser.name = @"admin";
        defaultUser.age = 20;
        defaultUser.sex = @1;
        defaultUser._id = 0;
        // 插入到数据库
        [defaultUser insertToDb];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 取出所有用户
    _users = [STUser allDbObjects];
    [_tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_users count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idetifier = @"UserListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idetifier];
    
    NSInteger row = [indexPath row];
    
    STUser *user = _users[row];
    
    cell.textLabel.text = user.name;
    
    cell.detailTextLabel.text = [NSString stringWithFormat:@"年龄:%d", user.age];
    
    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectedRow = [indexPath row];
    return indexPath;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    return row != 0;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSInteger row = [indexPath row];
        
        // 从数据库中删除数据
        STUser *user = _users[row];
        NSString *where = [NSString stringWithFormat:@"uid__=%d", user.uid__];
        if ([STUser removeDbObjectsWhere:where]) {
            // 数据库数据删除成功
            
            [tableView beginUpdates];
            
            // 删除数据源中的数据
            [_users removeObjectAtIndex:row];
            
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            [tableView endUpdates];
        }
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSString *idetifier = segue.identifier;
    
    if ([idetifier isEqualToString:@"userList_userDetail"]) {
        UserDetailViewController *detail = segue.destinationViewController;
        detail.user = _users[_selectedRow];
    }
}

- (void)addUser:(id)sender
{
    [self performSegueWithIdentifier:@"userList_addUser" sender:sender];
}

@end
